package stream;

import pojo.Student;

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GroupTest {
    public static void main(String[] args) {
        Stream<Student> stream = Stream.of(new Student("101", "猪八戒", 21),
                new Student("102", "孙悟空", 88),
                new Student("103", "撒僧", 50)
        );
        /**
         * Collectors.maxBy((o1,o2)->o1.getAge()- o2.getAge()) 是比较规则
         * collect 把结果收集起来放在一个容器中
         * Optional 容器
         * 返回的student 类型的对象   经过封装,放在了optional  类中
         */
        //获取最大值

//        Optional<Student> collect = stream.collect(Collectors.maxBy(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge()-o2.getAge();
//            }
//        }));
        //通过optional获取里面保存的对象
//        System.out.println(collect.get());
        //获取最小值
//        Optional<Student> collect2 = stream.collect(Collectors.minBy((o1, o2) -> o1.getAge() - o2.getAge()));
//        System.out.println(collect2.get());

//        //年龄求和
//        Integer collcet3 = stream.collect(Collectors.summingInt(s -> s.getAge()));
//        System.out.println(collcet3);

        //求平均值
//        Double collect4 = stream.collect(Collectors.averagingInt(x -> x.getAge()));
//        System.out.println(collect4);
        
        //统计数量
        Long collect = stream.collect(Collectors.counting());
        System.out.println("collect = " + collect);
    }
}
